package com.supor.aiot.mine.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.baseconfig.common.utils.ViewUtils;
import com.supor.aiot.mine.R;

import static android.util.TypedValue.COMPLEX_UNIT_PX;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: 我的功能Item <br>
 * 作者: lei <br>
 * 日期: 2021/8/19 <br>
 */
public class ServiceItemView extends RelativeLayout {

    private ImageView itemIcon;
    private TextView itemName;
    private ImageView dotIcon;

    public ServiceItemView(Context context) {
        this(context, null);
    }

    public ServiceItemView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ServiceItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ServiceItemView);

        // item名称
        String fivText = typedArray.getString(R.styleable.ServiceItemView_siv_text);
        int fiv_text_color = typedArray.getColor(R.styleable.ServiceItemView_siv_text_color, context.getResources().getColor(R
				.color.content_default));
        int fiv_text_size = typedArray.getDimensionPixelSize(R.styleable.ServiceItemView_siv_text_size, context
				.getResources().getDimensionPixelSize(R.dimen.sp_12));

        // 红点提示
        int fiv_dot_color = typedArray.getColor(R.styleable.ServiceItemView_siv_dot_color, context.getResources().getColor
                (R.color.FF3B30));
        int fiv_dot_size = typedArray.getDimensionPixelSize(R.styleable.ServiceItemView_siv_dot_size, -1);
        boolean fiv_dot_switch = typedArray.getBoolean(R.styleable.ServiceItemView_siv_dot_switch, false);

        // item图片
        int fiv_drawable = typedArray.getResourceId(R.styleable.ServiceItemView_siv_drawable, -1);

        typedArray.recycle();

        initView();

        itemName.setText(fivText);
        itemName.setTextSize(COMPLEX_UNIT_PX, fiv_text_size);
        itemName.setTextColor(fiv_text_color);

        if (fiv_dot_size != -1) {
            LayoutParams params = (LayoutParams) dotIcon.getLayoutParams();
            params.width = fiv_dot_size;
            params.height = fiv_dot_size;
            dotIcon.setLayoutParams(params);
        }
        dotIcon.setVisibility(fiv_dot_switch ? VISIBLE : GONE);

        if (fiv_drawable != -1) {
            ViewUtils.setViewVisible(itemIcon, View.VISIBLE);
            itemIcon.setImageResource(fiv_drawable);
        } else {
            ViewUtils.setViewVisible(itemIcon, View.GONE);
        }

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void initView() {
        View.inflate(getContext(), R.layout.item_more_service, this);
        itemIcon = (ImageView) findViewById(R.id.iv_image);
        itemName = (TextView) findViewById(R.id.tv_name);
        dotIcon = (ImageView) findViewById(R.id.iv_dot);
    }

    public ImageView getItemIcon() {
        return itemIcon;
    }

    public TextView getItemName() {
        return itemName;
    }

    public ImageView getDotIcon() {
        return dotIcon;
    }

    public void setItemName(String name) {
        itemName.setText(name);
    }

    public void setItemName(int name) {
        itemName.setText(getContext().getResources().getString(name));
    }

}
